// *** LICENSE HEADER ***
// Filename: ./resourcelist.cpp
// This file is part of Nodewarz 0.1
// Nodewarz is a 3d OpenGL strategy game
// Copyright (C) 2007 CZestmyr
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// *** END LICENSE HEADER ***
#include "resourcelist.h"

GLuint resourceList::textureAttack = 0;
GLuint resourceList::textureDefence = 0;
GLuint resourceList::textureLives = 0;
GLuint resourceList::textureCredits = 0;
GLuint resourceList::textureCircle = 0;
GLuint resourceList::textureTakeover = 0;

resourceList::resourceList(game* par){
    parent = par;
    resources = 0;
}

resourceList::~resourceList(){
    Flush();
}

void resourceList::Flush(){
    if (resources != 0){
        resource* it = resources;
        resource* toDel;
        while (it != 0){
            toDel = it;
            it = it->GetNext();
            delete toDel;
        }
    }
}

void resourceList::AddResource(float posX, float posY, int type, float radius, int ammount){
    //Create the resource
    resource* newRes = 0;
    switch(type) {
        case RT_ATTACK:
            newRes = new resourceAttack(posX, posY, radius, ammount, parent);
        break;
        case RT_DEFENCE:
            newRes = new resourceDefence(posX, posY, radius, ammount, parent);
        break;
        case RT_LIVES:
            newRes = new resourceLives(posX, posY, radius, ammount, parent);
        break;
        case RT_CREDITS:
            newRes = new resourceCredits(posX, posY, radius, ammount, parent);
        break;
        case RT_TAKEOVER:
            newRes = new resourceTakeover(posX, posY, radius, ammount, parent);
        break;
    }

    //Add the resource to the linked list
    if (newRes){
        newRes->SetNext(resources);
        resources = newRes;
    }
}

void resourceList::Grow(playerList* plist, int player){
    //Reset player's node growth data
    parent->GetPlayers()->GetPlayerPointer(player)->ResetGrowthData();

    resource* it = resources;
    while (it != 0){
        //First find out, which player has closest node to us and how close it is
        float closestDistance = FLT_MAX;
        int closestPlayer = -1;
        node* closestNode = 0;
        node* helper;
        for (int i = 0; i < plist->GetPlayerNum(); ++i){
            helper = plist->GetPlayerPointer(i)->GetClosestNode(it->GetX(), it->GetY());
            if (helper) {
                if (helper->CalculateDistanceTo(it->GetX(), it->GetY()) < closestDistance){
                    closestDistance = helper->CalculateDistanceTo(it->GetX(), it->GetY());
                    closestPlayer = i;
                    closestNode = helper;
                }
            }
        }
        //Then find out, if the node is close enough and Grow, if applicable
        if (closestDistance < _RESOURCE_RADIUS && closestPlayer == player)
            it->Grow(closestNode);

        it = it->GetNext();
    }
}

void resourceList::Draw(bool drawAmmounts){
    glDisable(GL_BLEND);
    glDisable(GL_TEXTURE_2D);
    glEnable(GL_DEPTH_TEST);

    //Get the font viewer
    fonts* fontViewer = parent->GetEngine()->GetFonts();
    //Get the opengl matrices
    if (drawAmmounts)
        resource::GetMatrices();

    //Draw all the resources
    resource* it = resources;
    while (it != 0){
        it->Draw(fontViewer, drawAmmounts);
        it = it->GetNext();
    }
}

void resourceList::DrawRadii(){
    glEnable(GL_BLEND);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, textureCircle);
    glDisable(GL_DEPTH_TEST);

    resource* it = resources;
    while (it != 0){
        it->DrawRadius();
        it = it->GetNext();
    }
}

void resourceList::LoadTextures(){
    textureAttack = ::LoadTexture("resattack.png");
    textureDefence = ::LoadTexture("resdefend.png");
    textureLives = ::LoadTexture("reslives.png");
    textureCredits = ::LoadTexture("rescredits.png");
    textureTakeover = ::LoadTexture("restakeover.png");
    textureCircle = ::LoadTexture("circle2.png");
}

void resourceList::UnloadTextures(){
    ::UnloadTexture(textureAttack);
    ::UnloadTexture(textureDefence);
    ::UnloadTexture(textureLives);
    ::UnloadTexture(textureCredits);
    ::UnloadTexture(textureTakeover);
    ::UnloadTexture(textureCircle);
}
